//
//  SNUserInfoModel.m
//  project
//
//  Created by 黄建华 on 2017/12/14.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNUserInfoModel.h"
#import "FileModelEntity.h"

@implementation SNWeatherModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        _weatherCode = [NSString string];
        _weatherInfo = [NSString string];
        _maxTem      = @"0";
        _minTem      = @"0";
        _tem         = @"0";
        _orderIndex = 0;
        _dateString = @"";
       
    }
     return self;
}
@end

@implementation SNAlarmModel
- (instancetype)init {
    self = [super init];
    if (self) {
        _orderIndex = 0;
        _open       = NO;
        _SUN        = NO;
        _SAT        = NO;
        _FRI        = NO;
        _THU        = NO;
        _WED        = NO;
        _TUE        = NO;
        _MON        = NO;
        _Once       = NO;
        _hours      = 0;
        _mins       = 0;
        _activeType = 0;
        _info       = @"";
        _date       = [NSDate date];
        _dateString = [_date dateToString];
    }
    return self;
}

@end

@implementation SNcalendarModel
- (instancetype)init {
    self = [super init];
    if (self) {
        _orderIndex = 0;
        _open       = NO;
        _year       = 2017;
        _month      = 0;
        _day        = 0;
        _hours      = 0;
        _mins       = 0;
        _activeType = 0;
        _info       = @"";
    }
    return self;
}

@end

@implementation SNUserInfoModel
- (instancetype)init {
    self = [super init];
    if (self) {
        _nickName      = @"昵称";
        _gender        = 1;
        _height        = 170;
        _weight        = 70;
        _birthday      = @"1990-01-01";
        _age                = 27;
        _job                = @"";
        _address            = @"";
        _sign               = @"很好";
        _isMetricSystem     = NO;
        _is12Time           = NO;
        _portrait            = @"";
        
        
        
        _heartFunction = NO;
        _oxFunction    = NO;
        _bpFunction    = NO;

        _weightTime    = @"";
        _registerDate  = [NSDate dateByStringFormat:@"2017-01-01 00:00:00"];
        [self setBMIValue];
        _target_step        = 10000;
        _target_sleep       = 8*60;
        NSInteger bmr = 0;
        if (_gender ==1) {
            bmr = 10*_weight +6.25*_height - 5*_age +5;
        }
        else
        {
            bmr = 10*_weight +6.25*_height - 5*_age -161;
        }
        bmr = (bmr/100+1)*100;
        _goal_calory        = bmr;
        
        _Instagram = YES;
        _Emails = YES;
        _Facebook = YES;
        _Wechat = YES;
        _Line = YES;
        _Weibo = YES;
        _Linkin = YES;
        _QQ = YES;
        _WhatsApp = YES;
        _Viber = YES;
        _Others = YES;
        
        _hand               = NO;
        _phoneNotice        = YES;
        _smsNotice          = YES;
        _ancsNotice         = YES;
        _lowElecNotice      = NO;
        _lostNotice         = NO;
        _liftNotice         = NO;
        _heartAutoCheck     = YES;
        _autoUpdateData     = YES;
        _temperatureSet     = NO;
        _weightunitSet      = NO;
        _appAutoUpdate      = NO;
        _changeScreen       = NO;
        _isFirstSetUserInfo = NO;
        _boolShoWeatherInfo = YES;
        _boolLightScreen    = NO;
        _heart              = NO;
        _oxygen             = NO;
        _bistoryPressure    = NO;
        _airPressure        = NO;
        _ancs               = NO;
        
        _isOpen = NO;
        _startH = 20;
        _startM = 0;
        _endH   = 8;
        _endM   = 0;
        
        _drinkOpen       = YES;
        _drinkSUN        = YES;
        _drinkSAT        = YES;
        _drinkFRI        = YES;
        _drinkTHU        = YES;
        _drinkWED        = YES;
        _drinkTUE        = YES;
        _drinkMON        = YES;
        _drinkOnce       = YES;
        _drinkStartHour1 = 9;
        _drinkStartMins1 = 0;
        _drinkEndHour1   = 18;
        _drinkEndMins1   = 0;
        _drinkStartHour2 = 0;
        _drinkStartMins2 = 0;
        _drinkEndHour2   = 0;
        _drinkEndMins2   = 0;
        _drinkStartHour3 = 0;
        _drinkStartMins3 = 0;
        _drinkEndHour3   = 0;
        _drinkEndMins3   = 0;
        _drinkinterval   = 30;
        _showDrinkTime = @"(09:00-18:00)";
        
        _sedentaryOpen       = YES;
        _sedentarySUN        = YES;
        _sedentarySAT        = YES;
        _sedentaryFRI        = YES;
        _sedentaryTHU        = YES;
        _sedentaryWED        = YES;
        _sedentaryTUE        = YES;
        _sedentaryMON        = YES;
        
        _sedentaryOnce       = YES;
        _sedentaryStartHour1 = 9;
        _sedentaryStartMins1 = 0;
        _sedentaryEndHour1   = 18;
        _sedentaryEndMins1   = 0;
        _sedentaryStartHour2 = 0;
        _sedentaryStartMins2 = 0;
        _sedentaryEndHour2   = 0;
        _sedentaryEndMins2   = 0;
        _sedentaryStartHour3 = 0;
        _sedentaryStartMins3 = 0;
        _sedentaryEndHour3   = 0;
        _sedentaryEndMins3   = 0;
        _sedentaryinterval   = 30;
        _showSedentaryTime = @"(09:00-18:00)";
        _checkTime = 30;

        _create_time = @"";
        _bleBatteryQuantity = 0;
        _showBleLost = YES;
        _showLanuage = NO;
        _wallpaper = NO;
        _isSupportSportMode = NO;
        _isSupportSettingTimeUnit = NO;
        _isSupportTianMao = NO;
        _isSupporFood   = NO;
        
        NSMutableArray *alarmArray    = [NSMutableArray array];
        NSMutableArray *calendarArray = [NSMutableArray array];
        for (int i = 0; i < 5; i++) {
            SNAlarmModel *alarm = [[SNAlarmModel alloc] init];
            alarm.orderIndex = i;
            [alarmArray addObject:alarm];
        }
        
        for (int i = 0; i < 5; i++) {
            SNcalendarModel *calendar = [[SNcalendarModel alloc] init];
            calendar.orderIndex = i;
            [calendarArray addObject:calendar];
        }
        
        for (int i = 0; i < 5; i++) {
            SNcalendarModel *calendar = [[SNcalendarModel alloc] init];
            calendar.orderIndex = i;
            [calendarArray addObject:calendar];
        }
        
        _alarmArray           = [NSMutableArray arrayWithArray:alarmArray];
        _calendarArray        = [NSMutableArray arrayWithArray:calendarArray];
        _historyCalendarArray = [NSMutableArray arrayWithArray:calendarArray];
        _lastUpdateTime = @"";
        
        _weatherArray = [NSMutableArray array];
        for (int i = 0; i <3 ; i++) {
            SNWeatherModel *model = [[SNWeatherModel alloc] init];
            model.orderIndex = i;
            [_weatherArray addObject:model];
        }
    }
    return self;
}

- (NSString *)showSign
{
    if (!_sign) {
        _sign = @"";
    }
     return _sign;
}

+ (SNUserInfoModel *)parseString:(NSString *)string {
    SNUserInfoModel *user = [string toModel:[self class]];
    
    return user;
}

- (void)setHeight:(NSInteger)height {
    _height = height;
    [self setBMIValue];
}

- (void)setWeight:(NSInteger)weight {
    _weight = weight;
    [self setBMIValue];
}

- (void)setBMIValue {
    CGFloat  BMIValue = _weight*1.0/(_height*1.00/100*_height*1.00/100);
    NSString *BMIStr  = [NSString string];
    
    if (BMIValue >= 32) {
        BMIStr = HHH_Text(@"非常肥胖");
    } else if (BMIValue >= 28) {
        BMIStr = HHH_Text(@"肥胖");
    } else if (BMIValue >= 24) {
        BMIStr = HHH_Text(@"过重");
    } else if (BMIValue >= 18.5) {
        BMIStr = HHH_Text(@"正常");
    } else {
        BMIStr = HHH_Text(@"过轻");
    }
    _BMI = [NSString stringWithFormat:@"%.1f  %@", BMIValue, BMIStr];
}

- (NSString *)currentAppVersion {
    _currentAppVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    if (!_currentAppVersion) {
        _currentAppVersion = [NSString string];
    }
    return _currentAppVersion;
}

- (void)setTarget_step:(NSInteger)target_step {
    _target_step = target_step;
    [self updateCurrentModelInDB];
}

- (void)setNickName:(NSString *)nickName {
    _nickName = nickName;
    _nickName = _nickName ? _nickName : @"";
}

- (void)setWeightTime:(NSString *)weightTime {
    _weightTime = weightTime;
    _weightTime = _weightTime ? _weightTime : @"";
}

- (NSMutableArray *)showCalendarArray {
    if (!_showCalendarArray) {
        _showCalendarArray = [NSMutableArray array];
    }
    return _showCalendarArray;
}

- (NSMutableArray *)historyArray {
    if (!_historyArray) {
        _historyArray = [NSMutableArray array];
    }
    return _historyArray;
}

- (NSMutableArray *)showAlarmArray {
    if (!_showAlarmArray) {
        _showAlarmArray = [NSMutableArray array];
    }
    return _showAlarmArray;
}

- (void)updateCurrentModelInDB {
    [SNUserInfoModel updateToDB:self where:nil];
}

+ (SNUserInfoModel *)getUserInfoFromDBWithUserID:(NSString *)userID {
    NSString        *where = [NSString stringWithFormat:@"user_id = '%@'", userID];
    SNUserInfoModel *model = [SNUserInfoModel searchSingleWithWhere:where orderBy:nil];
    
    if (!model) {
        model         = [[SNUserInfoModel alloc] init];
        model.user_id = userID;
        [model saveToDB];
    }
    
    return model;
}

// 表名
+ (NSString *)getTableName {
    return @"SNUserInfoModel";
}

// 主键
+ (NSString *)getPrimaryKey {
    return @"user_id";
}

// 表版本
+ (int)getTableVersion {
    return 1;
}

+ (void)initialize {
    [super initialize];
    [self bindYYJSONKey:@"id" toProperty:@"user_id"];
    [self bindYYJSONKey:@"nickname" toProperty:@"nickName"];
}

- (UIImage *)headImage {
    return SN_EveryDayModelHelp.localHeadImage;
}

- (NSString *)portrait {
    return _portrait ? _portrait : @"";
}

- (int)lanuage {
    int      value         = 0;
    NSArray  *appLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
    NSString *languageName = [appLanguages objectAtIndex:0];
    /*
    0x00:中文
    0x01:英文
    0x02:德文
    0x03:意大利文
    0x04:繁体中文
    0x05:西班牙文
    0x06:葡萄牙文
    0x07:日文
    0x08:法文
    0x09:俄罗斯文
    0x0A:土耳其文  10
    0x0B:乌克兰文  11
     */
    if ([languageName hasPrefix:@"zh"]) {
        value = 0;
    }
    else
    {
        value = 1;
    }
    
    return value;
}

// 新增多
- (int)lanuage2
{
//    ar-DZ hi vi te ta ru pl-PL ur fa ro-MD th-TH nl sk cs-CZ hu-HU
    NSArray  *appLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
    NSString *languageName = [appLanguages objectAtIndex:0];
    NSLog(@"languageName>>>%@",languageName);
    NSArray *array = @[@"zh-Hans",@"en",@"de",@"it",@"zh-Hant",@"es",@"pt",@"ja",@"fr",@"ru",@"tr",@"uk",@"ko",@"ar-DZ",@"hi",@"vi",@"te",@"ta",@"ur",@"pl-PL",@"ur",@"fa",@"ro-MD",@"th-TH",@"nl",@"sk",@"cs-CZ",@"hu-HU"];
    int      value         = 0;
    
    BOOL found = NO;
    for (int  k = 0; k< array.count; k++) {
        NSString *string = [array objectAtIndex:k];
        if ([languageName hasPrefix:string]) {
            value = k;
            found = YES;
            break;
        }
    }
    if (!found) {
        value = 1;
    }
   
    return value;
}

@end

